﻿using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevTeam;
using System;
using System.Drawing;

namespace ConfExpert.Forms.Aksesuar
{
    public partial class AksesuarLimit : XForm
    {
        public AksesuarLimit()
        {
            InitializeComponent();
        }

        private void AksesuarLimit_Load(object sender, EventArgs e)
        {
            gvAksesuar.Init("GetAksesuarLimit", WhoAmI);

            gvAksesuar.Columns["AksesuarId"].ColumnEdit = new RepositoryItemTextEdit();
            gvAksesuar.Columns["AksesuarId"].OptionsColumn.AllowEdit = false;
            gvAksesuar.Columns["AksesuarId"].AppearanceCell.BackColor = System.Drawing.Color.LightYellow;

            gvAksesuar.FormatAsNumber(0, "UyariLimiti", "KritikLimit");
            
            gvAksesuar.RowCellStyle += gvAksesuar_RowCellStyle;
            bbiGuncelle.ItemClick += bbiGuncelle_ItemClick;
            bbiKaydet.ItemClick += bbiKaydet_ItemClick;

            gvAksesuar.AddSummary();

            gvAksesuar.BestFitColumns();
        }

        void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAksesuar.EndEditing();

            string xml = gvAksesuar.SerializeChanges();
            ExecuteNonQuery("UpdAksesuarLimit", xml, WhoAmI);

            bbiGuncelle.PerformClick();
        }

        void bbiGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAksesuar.RefreshDataSource("GetAksesuarLimit", WhoAmI);
            gvAksesuar.BestFitColumns();
        }

        void gvAksesuar_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                switch (e.Column.FieldName)
                {
                    case "KritikLimit":
                        e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                        e.Appearance.ForeColor = Color.Red;
                        return;
                    case "UyariLimiti":
                        e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                        e.Appearance.ForeColor = Color.Orange;
                        return;
                }
            }
        }
    }
}
